package com.globalwave.common.db.gen;

import java.util.Map;

import com.globalwave.common.db.SimpleJDBCDAO;

public class SqlGeneratorAdapter {

    protected SimpleJDBCDAO dao = null ;
    
    private MapBeanSqlGenerator mapGenerator;
    private ObjectBeanSqlGenerator objectGenerator;
    
    public SqlGeneratorAdapter(SimpleJDBCDAO dao) {
    	this.dao = dao ;    	
    }

    public SQLGenerator getInstance(Object object) throws Exception {
	    if (object instanceof Map) {
	    	return this.getMapGenerator(object) ;
	 	} else {
	 		return getObjectGenerator(object) ;
	 	}
    }
    

    @SuppressWarnings("unchecked")
	public MapBeanSqlGenerator getMapGenerator(Object bean) {
    	if (mapGenerator == null) {
    		mapGenerator = new MapBeanSqlGenerator((Map<String, Object>)bean) ;
    		mapGenerator.dao = this.dao ;
    	} else {
    		mapGenerator.setBean((Map<String, Object>)bean) ;
    	}
		return mapGenerator;
	}
    
    public ObjectBeanSqlGenerator getObjectGenerator(Object bean) {
    	if (objectGenerator == null) {
    		objectGenerator = new ObjectBeanSqlGenerator(bean) ;
    		objectGenerator.dao = this.dao ;
    	} else {
    		objectGenerator.setBean(bean) ;
    	}
		return objectGenerator;
	}
}
